define(function(require, exports, module) {
  var $ = require("$");
  var dtUtil = require('datatableutil');
  var loader = require("./loader/loader");
  var bootbox = require("bootbox");
  require("bootstrap_commonrequire");
  require("moment");
  require('daterangepicker.css');
  require('bootstrap-daterangepicker');
  require('select2.css');
  require('select2');

  exports.init = function(param) {

    $(document).ready(function() {
      
      loader.loading("数据加载中……");

      // 加载自定义模板-条件区域
      var template = require("./logSms.tpl");
      var Widget = require("widget");
      var Component = Widget.extend({
        template : template,
      });
      new Component({
        renderTo : "queryAreaId"
      });

      function getCondition() {
        // 获取查询条件
        var queryStr = "";
        // cust_account
        var cust_account = $('#cust_account').val();
        if (cust_account != '' && cust_account != null) {
          queryStr += ("cust_account like '%" + cust_account + "%';");
        }	
        // 用户ID
        var user_identifer = $('#ext_terminalCode').val();
        if (user_identifer != '' && user_identifer != null) {
          queryStr += ("user_identifer like '%" + user_identifer + "%';");
        }
        // msg_id
        var msg_id = $('#msg_id').val();
        if (msg_id != '' && msg_id != null) {
          queryStr += ("msg_id like '%" + msg_id + "%';");
        } 
        // 内容
        var content = $('#mycontent').val();
        if (content != '' && content != null) {
          queryStr += ("content like '%" + content + "%';");
        }
        return queryStr;
      }
      // 加载表单模板和数据
      var Table = require("table");
      // 查询内容显示
      new Table({
        attrs : {
          id : "datatableid",
          isHovered : false,
          isStriped : true,
          isBordered : false
        },
        renderTo : param.divWrapperId

      });
      // 信息显示
      var data_table = $('#datatableid').dataTable({
        "sDom" : "<'hidden'<'span6'l><'hidden'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "bJQueryUI" : false,
        "oLanguage" : {
          "sUrl" : "/business/js/common/table/src/lang/dataTable.cn.txt"
        },
        "sPaginationType" : "full_numbers", // 分页自带两种样式，full_numbers和two_button(默认)
        'bPaginate' : true, // 是否分页
        "bProcessing" : true, // 当datatable获取数据时候是否显示正在处理提示信息。
        "bServerSide" : true, // 指定服务器端分页
        "bDeferRender" : true,
        "bFilter" : true,
        "bSort" : true,
        "bInfo" : true,
        "sAjaxSource" : param.ajaxUrl,
        "iDisplayLength" : DATATABLES_CONFIG.iDisplayLength.MIN, // 默认分页的每页记录数
        "fnServerData" : function(sSource, aoData, fnCallback, oSettings) {
          var queryparam = dtUtil.convert_aoData(aoData);

          // 拼接where查询条件
          var cond = getCondition();
          if (cond != '' && cond != null) {
            queryparam.q += cond;
          }
          // 添加自己的额外参数
          oSettings.jqXHR = $.ajax({
        	  "dataType" : 'json',
              "contentType" : "application/json;charset=UTF-8",
              "type" : "GET",
              "url" : sSource,
              "data" : queryparam, // 将请求数据转换为后台要求的JSON格式
            "success" : function(json) {
              loader.noloading();
              if (json.flag == '000000') {
                dtUtil.successCallback(json, aoData, fnCallback);
              } else {
                oauth2Relogin();
              }
            }
          });

        },
        "aoColumns" : [ {
          "sTitle" : "id",
          "bSortable" : false,
          "mData" : "id",
          "bVisible" : false
        }, {
            "sTitle" : "能力类型",
            "bSortable" : false,
            "mData" : "ability_type"
          }, {
              "sTitle" : "计费账号",
              "bSortable" : false,
              "mData" : "cust_account"
            },{
          "sTitle" : "用户Id",
          "bSortable" : false,
          "mData" : "user_identifer"
        },{
          "sTitle" : "msg_id",
          "bSortable" : false,
          "mData" : "msg_id"
        },{
          "sTitle" : "号码",
          "bSortable" : false,
          "mData" : "phones",
          "bVisible" : true
        },{
          "sTitle" : "发送状态",
          "bSortable" : false,
          "mData" : "status",
          "bVisible" : true,
          "mRender" : function(data, type, full) {
              if (data === "0")
                return "发送成功";
              else if (data === "-1")
                return "待发送";
              else if (data === "4")
            	  return "取消发送";
              else if (data === "3")
            	  return "已提交队列";
              else if (data === "2")
            	  return "信控审核中";
              else if (data === "1")
            	  return "发送失败"; 
              return data;
           }
       },{
	      "sTitle" : "内容",
	      "bSortable" : false,
	      "mData" : "content",
	      "bVisible" : true
	  },{
	      "sTitle" : "下发时间",
	      "bSortable" : false,
	      "mData" : "create_time",
	      "bVisible" : true
	  },{
          "sTitle" : "操作",
          "bSortable" : false,
          "sWidth" : "1%",
          "mData" : "status",
          "mRender" : function(data, state, full)
          {
        	  if(data === "1"){
        		  var ret = '<div class="btn-group" style="margin-top: 8px;">';
                  ret += '<a href="javascript:void(0);" class="btn icon-rocket contrast" title="重发"></a>';
                  return ret += '</div>';
        	  }else{
        		  return "";
        	  }
            
          }
        }],
        "fnDrawCallback" : function(oSettings) {
          // 表格绘制完成后，去除width属性，使表格可缩放
          $('#' + oSettings.sTableId).css("width", "");
        }
      });
      
      //单条的重发
      $("#data_table tbody").on("click", "tr>td>div>a.icon-rocket", function(e)
      {
        var rowNode = this.parentNode.parentNode.parentNode;
        var rowData = data_table.fnGetData(rowNode);
        if(rowData.status=='1'){
        	if(window.confirm('你确定要重发短信吗？')){
	        	loader.loading("正在提交，请稍等……");
	            $.ajax({
	              url : "/logSms/smsRepeat.do",//GLOBAL.URL.ADMIN_INSERTLICENSECARRIER,
	              dataType : "json",
	              contentType : "application/json;charset=UTF-8",
	              data : {
	            	'cust_account':"",
	            	'user_identifer':"",
	            	'msg_id':"",
	            	'content':"",
	            	'id':rowData.id
	              },
	              type : "GET",
	              success : function(json) {
	                if(json.flag == '000000'){
	                  loader.noloading();
	                  bootbox.alert(json.desc);
	                  data_table.fnFilter("");
	                } else {
	                  oauth2Relogin();                    
	                }
	              },
	              error : function(s) {
	                loader.noloading();
	                bootbox.alertTimeout("网络请求失败，请重试！");
	              }
	            });
        	}
        }else{
        	bootbox.alert("只有发送失败的记录才能重发");
        }
        
      });
      //条件补发
      $('#smsRepeat').click(function(){
    	  if(window.confirm('你确定要重发条件查询出来的所有短信吗？')){
    		  loader.loading("数据加载中……");
    		  $.ajax({
                  url : "/logSms/smsRepeat.do",//GLOBAL.URL.ADMIN_INSERTLICENSECARRIER,
                  dataType : "json",
                  contentType : "application/json;charset=UTF-8",
                  data : {
                	'cust_account':$('#cust_account').val(),
                	'user_identifer': $('#ext_terminalCode').val(),
                	'msg_id': $("#msg_id").val(),
                	'content':$("#mycontent").val(),
                	'id':''
                  },
                  type : "GET",
                  success : function(json) {
                    if(json.flag == '000000'){
                      loader.noloading();
                      bootbox.alert(json.desc);
                      data_table.fnFilter("");
                    } else {
                      oauth2Relogin();                    
                    }
                  },
                  error : function(s) {
                    loader.noloading();
                    bootbox.alertTimeout("网络请求失败，请重试！");
                  }
                });
           }
    	  return false;
      })
      // 查询按钮
      $('#querybtn').click(function() {
          loader.loading("数据加载中……");
    	  data_table.fnFilter("");
    	  return false;
      });

      // 重置按钮
      $('#restbtn').click(function() {
          loader.loading("数据加载中……");
          $('#cust_account').val("");
          $('#ext_terminalCode').val("");
          $("#msg_id").val("");
          $("#mycontent").val("");
          data_table.fnFilter("");
          return false;
      });
      
      

    });

  };
});
